<?php

namespace app\admin\model\video;

use think\Model;
use traits\model\SoftDelete;

class Video extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'video';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'name_text',
        'status_text',
        'is_pay_text',
        'createtime_text',
        'videoUrl'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    public function getVideoUrlAttr($value, $data)
    {
       if(isset($data['video_image']) && $data['video_image'] !== ''){
           $value = cdnurl($data['video_image']);
       }
       return $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        if(isset($data['createtime']) && $data['createtime']){
            $value = date('Y-m-d H:i:s',$data['createtime']);
        }
        return $value;
    }


    public function getNameList()
    {
        return ['block' => __('Block'), 'review' => __('Review'), 'pass' => __('Pass')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }

    public function getIsPayList()
    {
        return ['1' => __('Is_pay 1'), '2' => __('Is_pay 2'), '3' => __('Is_pay 3')];
    }


    public function getNameTextAttr($value, $data)
    {
        $value = $value ?: ($data['name'] ?? '');
        $list = $this->getNameList();
        return $list[$value] ?? '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getIsPayTextAttr($value, $data)
    {
        $value = $value ?: ($data['is_pay'] ?? '');
        $list = $this->getIsPayList();
        return $list[$value] ?? '';
    }




    public function contents()
    {
        return $this->belongsTo('Contents', 'video_contents_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
